package com.lanlian.cayman.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lanlian.commonlib.bean.UtilEnumInfo;
import com.lanlian.cayman.R;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/30 22:57
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
public class SpinnerView extends PopupWindow {

    private Context mContext;
    private View mContentView;
    private RecyclerView mRecycleView;
    private List<UtilEnumInfo> mList;
    private MyAdapter  mAdapter;

    public interface OnChooseListener {
        void onChoose(UtilEnumInfo utilEnumInfo);
    }

    private OnChooseListener mOnChooseListener;

    public SpinnerView(Context context, ArrayList<UtilEnumInfo> list, OnChooseListener onChooseListener) {
        this.mContext = context;
        this.mList = list;
        this.mOnChooseListener = onChooseListener;
        initView();
        setPopupWindow();
        initAdapter();
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.popup_spinner, null);
        mRecycleView = mContentView.findViewById(R.id.recycle_view);
    }

    private void initAdapter() {
        mAdapter = new MyAdapter(R.layout.adapter_spinner_utilenum, mList);
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycleView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mOnChooseListener.onChoose(mList.get(position));
                dismiss();
            }
        });
    }

    private void setPopupWindow() {
        setContentView(mContentView);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(0x00));
        setOutsideTouchable(false);
        setFocusable(true);
    }

    private class MyAdapter extends BaseQuickAdapter<UtilEnumInfo, BaseViewHolder> {

        public MyAdapter(int layoutResId, @Nullable List<UtilEnumInfo> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, UtilEnumInfo item) {
            helper.setText(R.id.tv_util_enum, item.getName());
        }
    }

}
